package com.xilin.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xilin.wiki.domain.Category;
import com.xilin.wiki.domain.CategoryExample;
import com.xilin.wiki.mapper.CategoryMapper;
import com.xilin.wiki.req.CategoryQueryReq;
import com.xilin.wiki.req.CategorySaveReq;
import com.xilin.wiki.response.CategoryQueryResp;
import com.xilin.wiki.response.PageResp;
import com.xilin.wiki.util.CopyUtil;
import com.xilin.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryService {
    private static final Logger LOG = LoggerFactory.getLogger(CategoryService.class);

    @Autowired()
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;

    public PageResp<CategoryQueryResp> list(CategoryQueryReq req) {

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        //内部类 where查询
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        //判断是否查询单个数据，否则全部展示


        PageHelper.startPage(req.getPage(), req.getSize());
        //查sql获得的<Category> list
        List<Category> list = categoryMapper.selectByExample(categoryExample);
        PageInfo<Category> pageInfo = new PageInfo<>(list);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());
        //List<Category> --> List<CategoryResp>
        List<CategoryQueryResp> respList = new ArrayList<>();

       /* for (Category category : list) {
          //CategoryResp categoryResp = new CategoryResp();
          //  BeanUtils.copyProperties(category,categoryResp);
          对象复制
            CategoryResp categoryResp = CopyUtil.copy(category, CategoryResp.class);
            respList.add(categoryResp);
        }*/

        //列表复制
        List<CategoryQueryResp> list1 = CopyUtil.copyList(list, CategoryQueryResp.class);

        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list1);
        return pageResp;
    }
    public List<CategoryQueryResp> all() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        List<Category> list = categoryMapper.selectByExample(categoryExample);
        //列表复制
        List<CategoryQueryResp> list1 = CopyUtil.copyList(list, CategoryQueryResp.class);

        return list1;
    }

    public void save(CategorySaveReq req) {
        Category category = CopyUtil.copy(req, Category.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            category.setId(snowFlake.nextId());
            categoryMapper.insert(category);
        } else {
            categoryMapper.updateByPrimaryKey(category);
        }
    }


    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }
}
